import { Controller, Post, Body, HttpCode, HttpStatus, Get, Query } from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateUserDto } from '../users/dto/create-user.dto';
import { LoginUserDto } from '../users/dto/login-user.dto';
import { SendSmsCodeDto } from '../users/dto/sms-code.dto';

@Controller('auth')
export class AuthController {
    constructor(private authService: AuthService) { }

    @Post('register')
    async register(@Body() createUserDto: CreateUserDto) {
        const user = await this.authService.register(createUserDto);
        return {
            success: true,
            message: '注册成功',
            data: user,
        };
    }

    @Post('login')
    @HttpCode(HttpStatus.OK)
    async login(@Body() loginUserDto: LoginUserDto) {
        const { token, user } = await this.authService.login(loginUserDto);
        return {
            success: true,
            message: '登录成功',
            data: {
                token,
                user,
            },
        };
    }

    @Post('send-sms-code')
    @HttpCode(HttpStatus.OK)
    async sendSmsCode(@Body() sendSmsCodeDto: SendSmsCodeDto) {
        const success = await this.authService.sendSmsCode(sendSmsCodeDto.phone);
        return {
            success,
            message: success ? '验证码发送成功' : '验证码发送失败',
        };
    }

    @Get('wechat-login-url')
    getWechatLoginUrl() {
        const url = this.authService.getWechatLoginUrl();
        return {
            success: true,
            data: { url },
        };
    }

    @Get('wechat-callback')
    async wechatCallback(@Query('code') code: string) {
        const { token, user } = await this.authService.wechatCallback(code);
        return {
            success: true,
            message: '微信登录成功',
            data: {
                token,
                user,
            },
        };
    }
} 